// pages/recruit/apply/apply.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //基本参数
    nickname: '',
    sex: '',
    telephone: '',
    weixin: '',
    anchor_type: '',
    roomid: '',
    job_plat: '',
    other_plat_name: '',
    position: '',
    conscribe_id: '',


    //界面显示
    type_name: [],
    type_id: [],
    plat_name: '',
    plat_id: '',

    //循环参数
    platfrom: [],
    recuitType: [],
    platfrom_nochange: [],
    recuitType_nochange: [],


    //开关
    select_type: true,
    select_plat: true,
    flag_weixin: false,
    flag_type: false,
    flag_job: false,
    flag_plat: false,
    flag_other: true,
    flag_room: false,
  },
  //重置
  click_reset() {
    this.setData({
      recuitType: this.data.recuitType_nochange,
      type_name: [],
      type_id: [],
    });

  },
  //点击其他平台
  click_otherPlat(e) {
    // console.log(e)
    this.setData({
      plat_id: e.currentTarget.dataset.plat_id,
      plat_name: "其他",
      platfrom: this.data.platfrom_nochange,
      flag_other: false,
    })

  },
  //选择平台
  click_a_plat(e) {
    // console.log(e)
    var index = e.currentTarget.dataset.platfromindex; //获取当前点击的下标
    var plat_name = e.currentTarget.dataset.plat_name;
    var plat_id = e.currentTarget.dataset.plat_id;
    var platfrom = this.data.platfrom; //选项集合    
    if (platfrom[index].checked) return; //如果点击的当前已选中则返回    
    platfrom.forEach(item => {
      item.checked = false
    })
    platfrom[index].checked = true; //改变当前选中的checked值    
    this.setData({
      platfrom: platfrom,
      plat_name: plat_name,
      plat_id: plat_id,
      flag_other: true,
    })
    // console.log(this.data.platfrom)
  },
  //选择主播类型
  click_a_type(e) {
    // console.log(e)
    let that = this;
    var index = e.currentTarget.dataset.recuittypeindex; //获取当前点击的下标
    var _type_name = e.currentTarget.dataset.type_name;
    var type_name = this.data.type_name;
    var _type_id = e.currentTarget.dataset.type_id;
    var type_id = this.data.type_id;
    var recuitType = this.data.recuitType; //选项集合

    var recuitType_text = this.data.recuitType_text;
    var type_id = this.data.type_id;

    if (type_id.length < 3) {
      //类型文字
      if (type_name == "") {
        type_name.push(_type_name);
        // console.log(1)
      } else {
        if (that.indexOf(type_name, _type_name) == "-1") {
          type_name.push(_type_name);
          // console.log(2)
        } else {
          that.remove(type_name, _type_name)
        }
      }
      //类型id
      if (type_id == "") {
        type_id.push(_type_id);
        // console.log(1)
      } else {
        if (that.indexOf(type_id, _type_id) == "-1") {
          type_id.push(_type_id);
          // console.log(2)
        } else {
          that.remove(type_id, _type_id)
        }
      }

      //选中状态
      if (recuitType[index].checked) {
        recuitType[index].checked = false; //改变当前选中的checked值 取消选择
      } else {
        recuitType[index].checked = true; //改变当前选中的checked值 确认选择
      }


      this.setData({
        recuitType: recuitType,
        type_name: type_name,
        type_id: type_id
      })
    } else {
      wx.showToast({
        title: '最多选择三个',
        icon: 'none',
        duration: 500,
      })
    }
    console.log(that.data.type_name)

  },
  //设置 原型
  indexOf(arr, val) {
    for (var i = 0; i < arr.length; i++) {
      if (arr[i] == val) return i;
    }
    return -1;
  },
  //设置 原型
  remove(arr, val) {
    var index = arr.indexOf(val);
    if (index > -1) {
      arr.splice(index, 1);
    }
  },

  //选择性别
  select_sex(e) {
    // console.log(e.currentTarget.dataset.sex);
    let sex = e.currentTarget.dataset.sex;
    this.setData({
      sex: sex
    })
  },
  //点击报名
  click_submit() {
    let type_id = this.data.type_id;
    if (typeof(type_id) == "object") {
      var anchor_type = type_id.join(",");
    } else {
      console.log(typeof(type_id))
    }
    if (this.data.nickname != "" && this.data.sex != "" && this.data.telephone != "") {
      if (this.data.plat_id == "0") {
        if (this.data.other_plat_name == "") {
          wx.showToast({
            title: '其他平台名不能为空',
            icon: 'none',
            duration: 500,
          })
        } else {
          this.sumbit(this.data.nickname, this.data.sex, this.data.telephone, this.data.weixin, anchor_type, this.data.roomid, this.data.plat_id, this.data.other_plat_name, this.data.position);
        }
      } else {
        this.sumbit(this.data.nickname, this.data.sex, this.data.telephone, this.data.weixin, anchor_type, this.data.roomid, this.data.plat_id, this.data.other_plat_name, this.data.position);
      }
    } else {
      wx.showToast({
        title: '姓名/性别/联系方式*必填',
        icon: 'none',
      })
    }
  },

  //关闭模态框
  modal_hide() {
    this.setData({
      select_type: true,
      select_plat: true,
    })
  },
  //点击 主播类型
  click_type() {
    this.setData({
      select_type: false,
    })
  },
  //点击 直播平台
  click_plat() {
    this.setData({
      select_plat: false,
    })
  },
  //发起提交请求
  sumbit(nickname, sex, telephone, weixin, anchor_type, roomid, job_plat, other_plat_name, position) {
    let that = this;
    wx.request({
      url: app.globalData.url + "api/conscribe/add",
      data: {
        token: app.globalData.token,
        conscribe_id: that.data.conscribe_id,
        nickname: nickname,
        sex: sex,
        telephone: telephone,
        weixin: weixin,
        anchor_type: anchor_type,
        roomid: roomid,
        job_plat: job_plat,
        other_plat_name: other_plat_name,
        position: position,
      },
      method: 'POST',
      success: function(e) {
        console.log(e)
        wx.showModal({
          content: '报名成功,点击返回~',
          showCancel: false,
          confirmText: '好',
          confirmColor: '#d0a76c',
          success(e) {
            wx.navigateBack({
              delta: 1,
            })
            // wx.redirectTo({
            //   url: '../announce/announce',
            // })

            // if (.confirm)
          }
        })
      },
    })
  },
  //输入
  inp_name(e) {
    // console.log(e.detail.value)
    this.setData({
      nickname: e.detail.value,
    })
  },
  inp_phone(e) {
    // console.log(e.detail.value)
    this.setData({
      telephone: e.detail.value,
    })
  },
  inp_wechat(e) {
    // console.log(e.detail.value)
    this.setData({
      weixin: e.detail.value,
    })
  },
  inp_job(e) {
    // console.log(e.detail.value)
    this.setData({
      position: e.detail.value,
    })
  },
  inp_other(e) {
    // console.log(e.detail.value)
    this.setData({
      other_plat_name: e.detail.value,
    })
  },
  inp_room(e) {
    // console.log(e.detail.value)
    this.setData({
      roomid: e.detail.value,
    })
  },
  //判断要显示的字段
  get_show() {
    let that = this;
    wx.request({
      url: app.globalData.url + "api/conscribe/add/need/field",
      data: {
        token: app.globalData.token,
        conscribe_id: that.data.conscribe_id,
      },
      method: 'POST',
      success: function(e) {
        // console.log(e.data.data.conscribe);
        if (e.data.code == "-2") {
          wx.redirectTo({
            url: '../../author/author',
          })
        }
        if (!e.data.data.conscribe) {
          console.log("不存在")
        } else {
          let conscribe_str = e.data.data.conscribe;
          let conscribe_arr = conscribe_str.split(",");
          if (conscribe_arr.indexOf("1") != "-1") {
            // $("#flag_weixin").show()
            that.setData({
              flag_weixin: true,
            })
          }
          if (conscribe_arr.indexOf("2") != "-1") {
            // $("#flag_type").show()
            that.setData({
              flag_type: true,
            })
          }
          if (conscribe_arr.indexOf("3") != "-1") {
            // $("#flag_plat").show()
            that.setData({
              flag_plat: true,
            })
          }
          if (conscribe_arr.indexOf("4") != "-1") {
            // $("#flag_room").show()
            that.setData({
              flag_room: true,
            })
          }
          if (conscribe_arr.indexOf("5") != "-1") {
            // $("#flag_job").show()
            that.setData({
              flag_job: true,
            })
          }
        }
      },
    })
  },
  //获取 平台信息等
  get_platInfo() {
    let that = this;
    wx.request({
      url: app.globalData.url + "api/recruit/platfrom/type",
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: function(e) {
        // console.log(e)

        //直播类型
        let recuitType_new = e.data.data.recuitType;
        let recuitType = [];
        recuitType_new.map(((item, index) => {
          recuitType.push(Object.assign(item, {
            checked: false
          }))
        }))
        //直播平台
        let platfrom_new = e.data.data.platfrom;
        let platfrom = [];
        platfrom_new.map(((item, index) => {
          platfrom.push(Object.assign(item, {
            checked: false
          }))
        }))
        that.setData({
          platfrom: platfrom,
          recuitType: recuitType,
          platfrom_nochange: platfrom,
          recuitType_nochange: recuitType,
        })
        // console.log(recuitType)
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let conscribe_id = options.conscribe_id;
    // console.log(conscribe_id + conscribe_id)
    this.setData({
      conscribe_id: conscribe_id
    })
    this.get_show();
    this.get_platInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})